<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
*/

/**
 * sfWidgetFormDate represents a date widget.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: nlWidgetFormHtml5Date.class.php 573 2010-12-27 10:42:42Z sn $
 */
class nlWidgetFormHtml5Date extends sfWidgetFormInput {
    /**
     * @param array $options An array of options
     * @param array $attributes An array of default HTML attributes
     *
     * @see sfWidgetForm
     */
    protected function configure($options = array(), $attributes = array ()) {
      parent::configure($options, $attributes);

      $this->addOption('culture', sfConfig::get('sf_default_culture', 'en'));
      $this->addOption('force_js', true);
      $this->addOption('with_time', false);
    }


  /**
   * Renders the widget.
   *
   * @param  string $name        The element name
   * @param  string $value       The value displayed in this widget
   * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
   * @param  array  $errors      An array of errors for the field
   *
   * @return string An HTML tag string
   *
   * @see sfWidgetForm
   */
  public function render($name, $value = null, $attributes = array(), $errors = array()) {

    $input_type = 'date';
    $date = substr($value,0,10);
    $time = substr($value,11,5);

    if ($this->getOption('force_js')) {
      $input_type = 'text';
    }

    $attributes += array(
      'type'  => $this->getOption('force_js') ? 'text' : 'date',
      'name'  => $name,
      'id'    => $this->generateId($name),
      'value' => $date
    );

    $attributes['class'] = isset($attributes['class']) ? $attributes['class'] . ' date_input' : 'date_input';
    $attributes['class'] = $this->getAttribute('class') ? $this->getAttribute('class') . ' '  .  $attributes['class'] : $attributes['class'];

    $timepicker = '';
    if ($this->getOption('with_time')) {
      $timepicker = $this->renderTag('input', array('class' => 'time_input', 'value' => $time, 'id' => $attributes['id'].'_time', 'type'=> 'text'));
      $timepicker.= $this->renderContentTag('script', "(function(){var t=$('#{$attributes['id']}_time').keyup(function(){if(this.value.match(/^\\d{3,4}$/)){var s=this.value.split('');s.splice(2,0,':');this.value=s.join('');}});t.closest('form').submit(function(){\$('#{$attributes['id']}')[0].value+= ' '+t.val();})})()", array('type' => 'text/javascript'));
    }


    return $this->renderTag('input', $attributes).$timepicker;
  }

  public function getJavaScripts() {
    $scripts = array('/nlSymfonyPlugin/js/jquery.date_picker.js');

    if ($this->getOption('culture') != 'en') {
      $scripts[] = '/nlSymfonyPlugin/js/jquery.date_picker.'. $this->getOption('culture') .'.js';
    }


    return array_unique(array_merge(parent::getJavaScripts(), $scripts));

  }

  public function getStylesheets() {
    return array('/nlSymfonyPlugin/css/jquery.date_picker.css' => 'screen');
  }

}